package com.ruoyi.service.achievements.impl;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.achievements.AcvScoringManageMapper;
import com.ruoyi.mapper.achievements.AcvTemplateManageMapper;
import com.ruoyi.service.achievements.AcvParameterService;
import com.ruoyi.service.achievements.AcvScoringManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.ObjectName;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 绩效打分服务实现类
 * @date 2020/8/24 16:59
 */
@Service
public class AcvScoringManageServiceImpl implements AcvScoringManageService {

    @Autowired
    AcvScoringManageMapper acvScoringManageMapper;

    @Autowired
    AcvTemplateManageMapper acvTemplateManageMapper;

    @Autowired
    AcvParameterService acvParameterService;

    @Override
    public List<Map<String, Object>> list(Map map) {
        map.put("superId", ShiroUtils.getSysUser().getUserId());
        return acvScoringManageMapper.list(map);
    }

    @Override
    public int validationIntroduction() {
        return acvScoringManageMapper.validationIntroduction(ShiroUtils.getSysUser().getUserId());
    }

    @Override
    public int introduce(String years, String months) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        //先循环人
        for (Map map1 : acvScoringManageMapper.findExamineeBySuperId(ShiroUtils.getSysUser().getUserId())) {
            Map map = new HashMap();
            map.put("examinee_id", map1.get("examinee_id").toString());
            //再用岗位遍历模板
            String job_id = map1.get("job_id").toString();
            String templateIdList = "";
            String scoreList = "";
            String remarkList = "";
            List<String> temp = acvScoringManageMapper.findTemplateByJobId(job_id);
            for (String tempId : temp) {
                templateIdList += tempId + ",";
                remarkList += "无@@";
                scoreList += "0,";
            }
            map.put("templateIdList", templateIdList.substring(0, templateIdList.length() - 1));
            map.put("scoreList", scoreList.substring(0, scoreList.length() - 1));
            map.put("remarkList", remarkList.substring(0, remarkList.length() - 2));
            map.put("years", years);
            map.put("months", months);
            mapList.add(map);
        }
        return acvScoringManageMapper.introduce(mapList);
    }


    @Override
    public int insertBill(String years, String months) {
        return acvScoringManageMapper.insertBill(years, months, ShiroUtils.getSysUser().getUserId());
    }

    @Override
    public List<Map<String, Object>> findScoringItems(String scoringId, String type) {
        //根据Scoring找到单条记录再循环
        Map map = acvScoringManageMapper.findScoringByScoringId(scoringId, type);
        String[] templateArray = map.get("template_id").toString().split(",");
        String[] remarkArray = map.get("remarks").toString().split("@@");
        String[] scoreArray = map.get("score").toString().split(",");
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < templateArray.length; i++) {
            String templateId = templateArray[i];
            Map map1 = acvTemplateManageMapper.findTemplateById(templateId);
            map1.put("remarks", remarkArray[i]);
            map1.put("score", scoreArray[i]);
            mapList.add(map1);
        }
        return mapList;
    }

    @Override
    public int dataSave(String scoreArray, String scoringId, String totalScore, String remarksTotal, String table) {
        List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(scoreArray);
        String templateIdList = "";
        String scoreList = "";
        String remarkList = "";
        for (Map<String, String> mapList : listObjectFir) {
            templateIdList += mapList.get("templateId").toString() + ",";
            scoreList += mapList.get("score").toString() + ",";
            remarkList += mapList.get("remarks").toString() + "@@";
        }
        templateIdList = templateIdList.substring(0, templateIdList.length() - 1);
        scoreList = scoreList.substring(0, scoreList.length() - 1);
        remarkList = remarkList.substring(0, remarkList.length() - 2);

        return acvScoringManageMapper.updateScoring(templateIdList, scoreList, remarkList, scoringId, totalScore, remarksTotal, table);
    }

    @Override
    public AjaxResult submitData() {
        //判断打分项是否有0
        if (acvScoringManageMapper.findScoreIsZero(ShiroUtils.getSysUser().getUserId()) > 0) {
            return new AjaxResult(AjaxResult.Type.ERROR, "存在未打分项,请进行打分后提交");
        }
        int num = 0;
        num = acvScoringManageMapper.submitData(ShiroUtils.getSysUser().getUserId());
        if (num > 0) {
            acvScoringManageMapper.deleteData(ShiroUtils.getSysUser().getUserId());
            return new AjaxResult(AjaxResult.Type.SUCCESS, "提交成功");
        }
        return new AjaxResult(AjaxResult.Type.ERROR, "提交失败,请确认是否引入考核人员!");
    }

    @Override
    public AjaxResult emptyData() {
        if (acvScoringManageMapper.emptyData(ShiroUtils.getSysUser().getUserId()) > 0) {
            return new AjaxResult(AjaxResult.Type.SUCCESS, "清空成功");
        } else {
            return new AjaxResult(AjaxResult.Type.ERROR, "清空失败,请确认是否有考勤数据");
        }
    }

    @Override
    public int isSubmitByYearsAndMonths(String years, String months) {
        return acvScoringManageMapper.isSubmitByYearsAndMonths(years, months, ShiroUtils.getSysUser().getUserId());
    }

    @Override
    public int updateTotalScore(String totalScore, String scoringId) {
        return acvScoringManageMapper.updateTotalScore(totalScore, scoringId);
    }

    @Override
    public Map findBillByBillId(String billId) {
        return acvScoringManageMapper.findBillByBillId(billId);
    }

    @Override
    public AjaxResult refreshPersonnel() {
        //判断是否引入本月数据
        if (acvScoringManageMapper.isImport(ShiroUtils.getSysUser().getUserId()) == 0) {
            return AjaxResult.error("未引入本月考核数据");
        }

        //查询未引入的列表
        List<Map<String, Object>> mapList = acvScoringManageMapper.findNotIntroducedList(ShiroUtils.getSysUser().getUserId());
        if (mapList.size() == 0) {
            return AjaxResult.error("未发现新入职人员");
        } else {
            List<Map<String, Object>> mapList1 = new ArrayList<>();
            String date = acvParameterService.findParameter("指定考核日期");
            for (Map map : mapList) {
                String job_id = map.get("job_id").toString();
                String templateIdList = "";
                String scoreList = "";
                String remarkList = "";

                List<String> temp = acvScoringManageMapper.findTemplateByJobId(job_id);
                for (String tempId : temp) {
                    templateIdList += tempId + ",";
                    remarkList += "无@@";
                    scoreList += "0,";
                }
                map.put("templateIdList", templateIdList.substring(0, templateIdList.length() - 1));
                map.put("scoreList", scoreList.substring(0, scoreList.length() - 1));
                map.put("remarkList", remarkList.substring(0, remarkList.length() - 2));
                map.put("years", date.split("-")[0]);
                map.put("months", date.split("-")[1]);
                mapList1.add(map);
            }
            acvScoringManageMapper.introduce(mapList1);
            return AjaxResult.success();
        }
    }
}
